﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SurfaceAd.Entities;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;

/**
 * 创建时间：2014/9/28 9:39:34
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace SurfaceAd
{
    public sealed partial class SurfaceAdPopup : UserControl
    {
        Popup popup = null;
        Grid LayoutRoot = new Grid() { Width = 480, Height = 80, Background = new SolidColorBrush(Colors.Red) };
        public SurfaceAdPopup()
        {
            this.LayoutRoot.Tapped += LayoutRoot_Tapped;
            this.Content = LayoutRoot;

            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame != null)
            {
            }
            try
            {
                Windows.Graphics.Display.DisplayInformation.GetForCurrentView().OrientationChanged += SurfaceAdPopup_OrientationChanged;
            }
            catch
            {
            }

        }

        void SurfaceAdPopup_OrientationChanged(Windows.Graphics.Display.DisplayInformation sender, object args)
        {
            if (popup != null)
            {
                OrientationScreenSize screenSize = OrientationScreenSizeHelper.GetScreenSize();
                popup.Height = screenSize.Height;
                popup.Width = screenSize.Width;
                //
                WebView webView = ((popup.Child as Grid).Children[0] as WebView);
                webView.Width = popup.Width;
                webView.Height = popup.Height;
            }
        }



        void LayoutRoot_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            if (popup == null)
            {
                popup = new Popup();
            }
            //
            OrientationScreenSize screenSize = OrientationScreenSizeHelper.GetScreenSize();
            popup.Height = screenSize.Height;
            popup.Width = screenSize.Width;
            //
            Grid main = new Grid();
            //
            WebView webView = new WebView();
            webView.Height = popup.Height;
            webView.Width = popup.Width;
            webView.Navigate(new Uri("http://www.baidu.com"));
            Button btn = new Button() { Content = "关闭", Background = new SolidColorBrush(Colors.Blue), Foreground = new SolidColorBrush(Colors.Red), Margin = new Thickness(0, 10, 10, 0), HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Right, VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top };
            btn.Tapped += delegate(object senderBtn, Windows.UI.Xaml.Input.TappedRoutedEventArgs eBtn)
            {
                if (popup != null)
                {
                    popup.IsOpen = false;
                    popup = null;
                }
            };
            //
            main.Children.Add(webView);
            main.Children.Add(btn);
            popup.Child = main;
            //
            popup.IsOpen = true;
        }

    }
}
